Java Technologies Datagram Socket Example: UDP Protocol এর মাধ্যমে Data Transmission গাইড ও নোট

337

এখানে একটি Datagram Socket Example দেওয়া হল যা UDP Protocol এর মাধ্যমে ডেটা ট্রান্সমিশন দেখায়। UDP (User Datagram Protocol) একটি connectionless protocol যা কম দেরিতে এবং ছোট ডেটা প্যাকেট প্রেরণ করে থাকে।

১. UDP Client (Sender) Example

এই কোডটি UDP সেক্টরে ডেটা পাঠানোর জন্য একটি ক্লায়েন্ট অ্যাপ্লিকেশন তৈরি করবে।

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPClient {
    public static void main(String[] args) {
        DatagramSocket socket = null;
        try {
            // Create DatagramSocket to send data
            socket = new DatagramSocket();
            
            String message = "Hello from UDP Client!";
            byte[] sendData = message.getBytes();
            
            // Get the server address and port
            InetAddress serverAddress = InetAddress.getByName("localhost");
            int serverPort = 9876;
            
            // Create a DatagramPacket with message and send to server
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, serverPort);
            socket.send(sendPacket);
            
            System.out.println("Message sent to server: " + message);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (socket != null && !socket.isClosed()) {
                socket.close();
            }
        }
    }
}

২. UDP Server (Receiver) Example

এই কোডটি UDP সেক্টরে ক্লায়েন্ট থেকে ডেটা গ্রহণ করবে এবং তা প্রিন্ট করবে।

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UDPServer {
    public static void main(String[] args) {
        DatagramSocket socket = null;
        try {
            // Create DatagramSocket to receive data
            socket = new DatagramSocket(9876);  // Server listens on port 9876
            
            byte[] receiveData = new byte[1024];
            
            while (true) {
                // Create DatagramPacket to receive the message
                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                socket.receive(receivePacket);
                
                // Extract message from packet
                String message = new String(receivePacket.getData(), 0, receivePacket.getLength());
                
                // Print received message
                System.out.println("Received message: " + message);
            }
        } catch (SocketException e) {
            System.out.println("Socket error: " + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (socket != null && !socket.isClosed()) {
                socket.close();
            }
        }
    }
}

ব্যাখ্যা:

  1. UDPClient (Sender):
    • একটি DatagramSocket তৈরি করা হয় যা ডেটা পাঠানোর জন্য ব্যবহৃত হয়।
    • InetAddress.getByName("localhost") ব্যবহার করে আমরা সার্ভারের IP ঠিকানা পেতে পারি।
    • ক্লায়েন্ট একটি UDP প্যাকেট তৈরি করে এবং তা সার্ভারের কাছে পাঠিয়ে দেয়।
  2. UDPServer (Receiver):
    • সার্ভার একটি DatagramSocket দিয়ে একটি নির্দিষ্ট পোর্ট (যেমন, 9876) তে মেসেজ গ্রহণ করতে থাকে।
    • এটি একটি DatagramPacket ব্যবহার করে ডেটা গ্রহণ করে এবং তারপর সেই মেসেজটি কনসোলে প্রিন্ট করে।

UDP Protocol:

  • UDP একটি connectionless protocol, যার মানে এটি প্রথমে কোনো কনেকশন স্থাপন না করেই ডেটা প্রেরণ করে এবং গ্রহণ করে।
  • এটি অপেক্ষা বা নিশ্চিতকরণ ছাড়াই দ্রুত ডেটা ট্রান্সফার করতে পারে, তবে এর মাধ্যমে ডেটার ক্ষতি বা লস হতে পারে।
Content added By
Promotion

Are you sure to start over?

Loading...